package cn.darkhorse.workorder.service.impl;

import java.util.List;

import cn.darkhorse.workorder.domain.QmsWorkorderHvi;
import cn.darkhorse.workorder.mapper.QmsWorkorderHviRecordMapper;
import cn.darkhorse.workorder.service.IQmsWorkorderHviRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import cn.darkhorse.workorder.domain.QmsWorkorderHviRecord;
import cn.darkhorse.workorder.mapper.QmsWorkorderHviMapper;
import cn.darkhorse.workorder.service.IQmsWorkorderHviService;

/**
 * hvi检验Service业务层处理
 *
 * @author ytq
 * @date 2024-07-19
 */
@Service
public class QmsWorkorderHviServiceImpl extends ServiceImpl<QmsWorkorderHviMapper, QmsWorkorderHvi> implements IQmsWorkorderHviService
{
    @Autowired
    private QmsWorkorderHviMapper qmsWorkorderHviMapper;
    @Autowired
    private QmsWorkorderHviRecordMapper qmsWorkorderHviRecordMapper;
    @Autowired
    private IQmsWorkorderHviRecordService hviRecordService;



    @Override
    public QmsWorkorderHvi getByOrderNumber(String orderNumber) {
        return lambdaQuery().eq(QmsWorkorderHvi::getOrderNumber, orderNumber).one();
    }



    /**
     * 新增hvi检验
     *
     * @param qmsWorkorderHvi hvi检验
     * @return 结果
     */
    @Transactional
    @Override
    public boolean saveQmsWorkorderHvi(QmsWorkorderHvi qmsWorkorderHvi) {
        insertOrUpdateQmsWorkorderHviRecord(qmsWorkorderHvi);
        return saveOrUpdate(qmsWorkorderHvi);
    }




    /**
     * 新增hvi检验记录
信息
     *
     * @param qmsWorkorderHvi hvi检验对象
     */
    public void insertOrUpdateQmsWorkorderHviRecord(QmsWorkorderHvi qmsWorkorderHvi)
    {
        List<QmsWorkorderHviRecord> qmsWorkorderHviRecordList = qmsWorkorderHvi.getQmsWorkorderHviRecordList();
        Long id = qmsWorkorderHvi.getId();
        if (StringUtils.isNotNull(qmsWorkorderHviRecordList)) {
            List<QmsWorkorderHviRecord> list = new ArrayList<QmsWorkorderHviRecord>();
            for (QmsWorkorderHviRecord qmsWorkorderHviRecord : qmsWorkorderHviRecordList) {
                qmsWorkorderHviRecord.setHviId(id);
                qmsWorkorderHviRecord.setOrderNumber(qmsWorkorderHvi.getOrderNumber());
                if (qmsWorkorderHviRecord.getId()!=null){
                    qmsWorkorderHviRecordMapper.updateById(qmsWorkorderHviRecord);
                }else {
                    list.add(qmsWorkorderHviRecord);
                }
            }
            if (!list.isEmpty()) {
                qmsWorkorderHviMapper.batchQmsWorkorderHviRecord(list);
            }
        }
    }
}
